
##########################################################################################

# Description: Replicating analysis from Fleming, Thomas G. and James, Lisa (2022) 
#              'Parliamentary Influence on Brexit Legislation, 2017-2019',
#              Parliamentary Affairs, Advance article.
# Authors:     Thomas G. Fleming and Lisa James
# Date:        21/06/2022
# Note:        Requires file "amendments.csv" to be saved in same folder

##########################################################################################

rm(list=ls())

library(xtable)
library(rstudioapi)
current_path <- getActiveDocumentContext()$path
setwd(dirname(current_path))

###################################
# Loads data and separates Houses #
###################################

  amendments <- read.csv("./amendments.csv")

  commons <- amendments[(amendments$stage == "CCLA" | 
                         amendments$stage == "CCLM" | 
                         amendments$stage == "Commons Committee" | 
                         amendments$stage == "Commons Report"),]

  lords <- amendments[(amendments$stage == "LCCA" |
                       amendments$stage == "LCCR" |
                       amendments$stage == "Lords Committee" |
                       amendments$stage == "Lords Report" |
                       amendments$stage == "Lords Third Reading"),]
  
##############################################################################
# Table 2 - showing distribution of Commons amendment outcomes by supporters #
##############################################################################

  # creating table, naming total column and row, and sorting rows into meaningful order
  
  tab2.unformatted <- xtable(addmargins(table(commons$party, commons$outcome, exclude = NULL)))
  colnames(tab2.unformatted)[ncol(tab2.unformatted)] <- "Total"
  rownames(tab2.unformatted)[nrow(tab2.unformatted)] <- "Total"
  tab2.unformatted <- tab2.unformatted[c(2,3,7,4,5,8,6,1,9),]
  
  # adding row percentages to table
  
  tab2 <- tab2.unformatted
  for (z in seq(1:ncol(tab2))) {
    for (i in seq(1:nrow(tab2))) {
      tab2[i, z] <- paste(as.character(tab2.unformatted[i,z]), " (", 
                          as.character(round(100*(tab2.unformatted[i,z]/tab2.unformatted[i,ncol(tab2)])), digits = 2), 
                          "%)", sep = "")
    }
  }
  rm(tab2.unformatted, i, z)
  
  # formatting and printing

  align(tab2) <- "lcccc"
  caption(tab2) <- "Table 2. Outcomes of Commons amendments to Brexit legislation"
  
  print(tab2,
        type = "html",
        file = "./table2.html")
  
############################################################################
# Table 3 - showing distribution of Lords amendment outcomes by supporters #
############################################################################
  
  # creating table, naming total column and row, and sorting rows into meaningful order
  
  tab3.unformatted <- xtable(addmargins(table(lords$party, lords$outcome, exclude = NULL)))
  colnames(tab3.unformatted)[ncol(tab3.unformatted)] <- "Total"
  rownames(tab3.unformatted)[nrow(tab3.unformatted)] <- "Total"
  tab3.unformatted <- tab3.unformatted[c(2,3,7,4,5,6,9),]
  
  # adding row percentages to table
  
  tab3 <- tab3.unformatted
  for (z in seq(1:ncol(tab3))) {
    for (i in seq(1:nrow(tab3))) {
      tab3[i, z] <- paste(as.character(tab3.unformatted[i,z]), " (", 
                          as.character(round(100*(tab3.unformatted[i,z]/tab3.unformatted[i,ncol(tab3)])), digits = 2), 
                          "%)", sep = "")
    }
  }
  rm(tab3.unformatted, i, z)
  
  # formatting and printing
  
  align(tab3) <- "lcccc"
  caption(tab3) <- "Table 3. Outcomes of Lords amendments to Brexit legislation"
  
  print(tab3,
        type = "html",
        file = "./table3.html")
  
#########################################################################
# Table 4 - showing substantiveness of successful amendments by supporters #
#########################################################################
  
  # subsetting on only successful amendments which weren't overturned or overturning without replacement
  
  successful <- amendments[(amendments$outcome == "Agreed" &
                            amendments$overturning != "Overturning" &
                            amendments$overturned == "Not overturned"),]
  
  # creating table and sorting rows into meaningful order
  
  tab4.unformatted <- xtable(addmargins(table(successful$party, successful$substantiveness, exclude = NULL)))
  
  colnames(tab4.unformatted) <- c("Clarificatory", "Substantive", "Technical", "Total")
  rownames(tab4.unformatted)[nrow(tab4.unformatted)] <- "Total"
  
  tab4.unformatted <- tab4.unformatted[c(2,3,7,4,5,8,9),c(2,1,3)]
  
  # adding column percentages to table
  
  tab4 <- tab4.unformatted
  
  for (z in seq(1:ncol(tab4))) {
    for (i in seq(1:nrow(tab4))) {
      tab4[i, z] <- paste(as.character(tab4.unformatted[i,z]), 
                          " (", 
                          as.character(round(100*(tab4.unformatted[i,z]/tab4.unformatted[nrow(tab4),z])), digits = 2), 
                          "%)", sep = "")
    }
  }
  
  rm(tab4.unformatted, i, z)
  
  # formatting and printing
  
  align(tab4) <- "lccc"
  caption(tab4) <- "Table 4. Substantiveness of successful amendments to Brexit legislation"
  
  print(tab4,
        type = "html",
        file = "./table4.html")
  
####################################################
# Table 5 - linking amendments to earlier pressure #
#################################################### 

  # creating datasets of successful substantive gov amendments for each chamber
  
  pressure.commons <- commons[which(commons$substantiveness == "substantive" & 
                                      commons$party == "Government" &
                                      commons$outcome == "Agreed" &
                                      commons$overturning != "Overturning" &
                                      commons$overturned == "Not overturned"),]
  
  
  pressure.lords <- lords[which(lords$substantiveness == "substantive" & 
                                  lords$party == "Government" &
                                  lords$outcome == "Agreed" &
                                  lords$overturning != "Overturning" &
                                  lords$overturned == "Not overturned"),]
  
  # creating very simple table for each chamber
  
  table(pressure.commons$pressure, exclude = NULL)
  
  table(pressure.lords$pressure, exclude = NULL)
  
########################################################################################## 
  
  rm(list=ls())
  